<script lang="ts" setup>
import Inputter from "@/components/Inputter.vue";
import instance from '@/utils/request';
import { useEmployeeStore } from "@/stores/employee";
import router from "@/router/router";
import { ref, onMounted } from "vue";
import { ElMessage } from 'element-plus';

// 员工历史订单页面
onMounted(() => {
  init();
});

const employee = useEmployeeStore();

// 搜索
const order_id_select = ref("");
const order_room_id_select = ref("");
const order_stay_name_select = ref("");
const order_start_time_select = ref("");
const order_end_time_select = ref("");
const order_employee_name_select = ref("");

interface Data {
  order_id: string
  order_room_id: string
  order_stay_name: string
  order_start_time: string
  order_end_time: string
  order_employee_name: string
}

// 不同状态不同样式
const rowClassName = ({row, rowIndex,}: {
  row: Data
  rowIndex: number
}) => {
  return 'color-employee-history-1';
}

// 数据
let tableData = [];

// 条件查询
const filterTableData = ref();
function filter() {
  filterTableData.value = tableData.filter((data) =>
      (!order_id_select.value || data.order_id.toString().includes(order_id_select.value)) &&
      (!order_room_id_select.value || data.order_room_id.toString().includes(order_room_id_select.value)) &&
      (!order_stay_name_select.value || data.order_stay_name.toString().includes(order_stay_name_select.value)) &&
      //(!order_start_time_select.value || data.order_start_time.includes(order_start_time_select.value)) &&
      //(!order_end_time_select.value || data.order_end_time.includes(order_end_time_select.value)) &&
      (!order_employee_name_select.value || data.order_employee_name.includes(order_employee_name_select.value))
  );
}

function init() {
  // 初始化
  instance.request({
    url: 'employee/history/init',
    method: 'POST',
    headers: { "Employee-Id": employee.employee_id }
  })
      .then(function(response) {
        if(response.data.code === "ERROR") {
          ElMessage({
            message: "初始化失败！",
            type: "error"
          });
        }
        if(response.data.code === "SUCCESS") {
          tableData = response.data.data;
          filterTableData.value = tableData;
        }
      })
      .catch(function(error){
        console.log(error);
        if(error.response.request.status === 401) {
          ElMessage({
            message: "您的连接已被注销！",
            type: "error"
          });
          employee.logout();
          router.push("/employee/index");
          return;
        }
        ElMessage({
          message: "初始化失败！",
          type: "error"
        });
        setTimeout(() => {
          init();
        }, 2000 );
      });
}
</script>

<template>
  <div class="select">
    <span>订单号：</span>
    <inputter width="100" v-model="order_id_select"/>

    &emsp;&emsp;<span>房间号：</span>
    <inputter width="100" v-model="order_room_id_select"/>

    &emsp;&emsp;<span>入住人：</span>
    <inputter width="100" v-model="order_stay_name_select"/>

    &emsp;&emsp;<span>负责人：</span>
    <inputter width="100" v-model="order_employee_name_select"/>

    <!--
    <br><br><span>入住时间：</span>
    <inputter width="200" v-model="order_start_time_select"/>

    &emsp;&emsp;<span>退房时间：</span>
    <inputter width="200" v-model="order_end_time_select"/>
    -->

    &emsp;&emsp;
    <el-button color="#568fc1" class="button-color" @click="filter">
      <span>搜索</span>
    </el-button>
  </div>

  <div class="table">
    <el-table
        header-row-style="color: #000000; background-color: #f0f0f0;"
        height="600"
        style="width: 100%"
        :data="filterTableData"
        :row-class-name="rowClassName"
    >
      <el-table-column prop="order_id" label="订单号" width="150"/>
      <el-table-column prop="order_room_id" label="房间号" width="150"/>
      <el-table-column prop="order_stay_name" label="入住人" width="150"/>
      <el-table-column prop="order_start_time" label="入住时间" width="240"/>
      <el-table-column prop="order_end_time" label="退房时间" width="240"/>
      <el-table-column prop="order_employee_name" label="负责人" width="150"/>
    </el-table>
  </div>
</template>

<style scoped>
.select {
  padding-top: 2vh;
  padding-left: 8vh;
  padding-right: 5vh;
  /* border: 1px solid black; */
}
.table {
  padding: 1vh 5vh 5vh;
  /* border: 1px solid black; */
}
.button-color {
  --el-button-hover-bg-color: rgb(238, 244, 249) !important;
  --el-button-hover-text-color: #568fc1 !important;
  --el-button-text-color: white !important;
}
</style>

<style>
.el-table .color-employee-history-1 {
  --el-table-tr-bg-color: #e4f7fb;
  --el-table-row-hover-bg-color: #e4f7fb;
}
</style>